package gases.as3.librarys.components.ui.processbar
{
	import flash.display.BitmapData;
	
	import gases.as3.librarys.components.ui.GSkinBox;
	import gases.as3.librarys.components.ui.GUIComponents;
	
	public class GProcessBar extends GUIComponents
	{
		public function GProcessBar()
		{
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			_backImage = new GSkinBox();
			_processImage = new GSkinBox();
			
			this.addChild(_backImage);
			this.addChild(_processImage);
			
			_backImage.source = new BitmapData(200,20,false,0x000000);
			_processImage.source = new BitmapData(200,20,false,0xff0000);
		}
		/**
		 *设置大小 
		 * @param w
		 * @param h
		 * 
		 */		
		public function setWH(w:Number = 200,h:Number = 20):void
		{
			_backImage.Gheight = h;
			_backImage.Gwidth = w;
			_processImage.Gheight = h;
			_processImage.Gwidth = w;
			_backImage.source = _backImage.source;
			_processImage.source = _processImage.source;
		}
		/**
		 *设置背景资源 
		 * @param obj
		 * 
		 */		
		public function setBackgroundImage(obj:*):void
		{
			if(!obj)
			{
				 return;
			}
			_backImage.source = obj;
		}
		
		/**
		 *设置显示资源 
		 * @param obj
		 * 
		 */		
		public function setProcessBarImage(obj:*):void
		{
			if(!obj)
			{
				return;
			}
			_processImage.source = obj;
		}
		
		/**
		 *设置参数，0-1之间 
		 * @param value
		 * 
		 */		
		public function set value(num:Number):void
		{
			_value = Math.min(num,_max);
			if(_value < 0 )
			{
				_value = _min;
			}
			updateUI();
		}
		
		public function get value():Number
		{
			return _value;
		}
		/**
		 *设置最大值，最大为1; 
		 * @param num
		 * 
		 */		
		public function set max(num:Number):void
		{
			_max = num;
			if(_max < 0)
			{
				_max = _min;
			}
			_value = Math.min(_value,_max);
			updateUI();
		}
		
		public function get max():Number
		{
			return _max;
		}
		
		private function updateUI():void
		{
			_processImage.scaleX = _value / _max;
		}
		
		override public function dispose():void
		{
			super.dispose();
			if(this.contains(_backImage))
			{
				this.removeChild(_backImage);
			}
			if(this.contains(_processImage))
			{
				this.removeChild(_processImage);
			}
		}
		
		
		private var _value:Number;
		private var _max:Number = 1.0;
		private var _min:Number = 0.0;
		
		
		private var _backImage:GSkinBox;
		private var _processImage:GSkinBox;
	}
}